<?php

namespace App\Api\PhoneCheck\AliPhone;

/**
 * https://docs.qq.com/doc/DRUlEamNJWFFhVmVR
 */

use App\Api\PhoneCheck\ChannelApiCheck;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Support\Facades\DB;


class PhoneCheck extends ChannelApiCheck
{
    private $log;
    public function __construct($url = '', $str = '')
    {
        $this->log = new Logger('PhoneCheckApi');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/PhoneCheckApi.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }
    public function callBackData($data = [])
    {
        if (empty($data['click_id'])) {
            return $this->returnMsg(104, '参数错误');
        }
        $url = 'https://analytics.oceanengine.com/api/v2/conversion';
        $dataSend = [
            'event_type' => 'form',
            'context' => [
                'ad' => [
                    'callback' => $data['click_id'],
                ],
            ],
            'timestamp' => getMillisecond()
        ];
        $res = $this->send($url, $dataSend);
        $this->log->info('form参数:' . json_encode($data) . '返回参数:' . $res);
        $dataSend = [
            'event_type' => 'active_pay',
            'context' => [
                'ad' => [
                    'callback' => $data['click_id'],
                ],
            ],
            'timestamp' => getMillisecond()
        ];
        $res = $this->send($url, $dataSend);
        $this->log->info('active_pay参数:' . json_encode($data) . '返回参数:' . $res);
        if (!empty($res)) {
            $res = json_decode($res, true);
            if (isset($res['code']) && $res['code'] == 0) {
                return $this->returnMsg(200, '回传成功');
            }
        }
        return $this->returnMsg(109, '发生未知错误');
    }
    public function send($phone)
    {
        $host = "https://ispcheck.market.alicloudapi.com";
        $path = "/switchnetwork";
        $method = "POST";
        $appcode = "9571fddb5db14b0b9d202acf2b5ffb07";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "mobile=" . $phone;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $return = curl_exec($curl);
        $this->log->info('手机参数:' . $phone . '返回参数:' . $return);
        return $return;
    }
}
